package com.jczy.cyclone.mmkv

import com.jczy.lib_utils.mmkv.MMKVNullableProperty
import com.jczy.lib_utils.mmkv.MMKVOwner
import com.jczy.lib_utils.mmkv.MMKVProperty
import com.tencent.mmkv.MMKV

/**
 *@author ZengCS
 *@description 商城相关数据缓存
 *@date 2023/4/9 23:05
 */
object MallMMKV : MMKVOwner {
    override val mmkv: MMKV
        get() = MMKV.mmkvWithID(MMKVConfig.ID.MALL)

    // 上次加载商城广告的时间，确保每天只加载一次
    private var lastShowMallHomeAdDate by MMKVProperty("")
    private var lastShowMallFloatAdDate by MMKVProperty("")
    private var lastShowLongDouSignDate by MMKVProperty("")

    var lastCheckedShoppingCartCodes by MMKVProperty(ArrayList<String>())

    // 判断今天是否需要展示商城首页广告
//    fun needShowMallHomeAd(home: Boolean): Boolean {
//        println("needShowMallHomeAd today() = ${today()}")
//        return if (home) lastShowMallHomeAdDate != today() else lastShowMallFloatAdDate != today()
//    }
//
//    // 判断今天是否需要展示签到弹窗
//    fun needShowLongDouSign(): Boolean {
//        println("needShowLongDouSign today() = ${today()}")
//        return lastShowLongDouSignDate != today()
//    }
//
//    fun cacheLastShowLongDouDate() {
//        lastShowLongDouSignDate = today()
//    }
//
//    fun cacheLastShowMallHomeAdDate(home: Boolean) {
//        println("cacheLastShowMallHomeAdDate today() = ${today()}")
//        if (home)
//            lastShowMallHomeAdDate = today()
//        else
//            lastShowMallFloatAdDate = today()
//    }

    //var openApiLoginResp by MMKVNullableProperty(SysLoginResponse::class.java)

    /** 购物车选中缓存--start */
    private val mCheckedCodes = ArrayList<String?>()
    private var cacheCartCheckedCodes by MMKVNullableProperty(CacheCartCodes::class.java)

    fun initCheckedCodes() {
        mCheckedCodes.clear()
        mCheckedCodes.addAll(cacheCartCheckedCodes?.list ?: emptyList())
    }

    fun clearCacheCheckedCodes(codes: List<String?>? = null) {
        cacheCartCheckedCodes = if (codes.isNullOrEmpty()) {
            mCheckedCodes.clear()
            CacheCartCodes()
        } else {
            mCheckedCodes.removeIf { it in codes }
            CacheCartCodes(mCheckedCodes)
        }
    }

    fun cacheCheckedCodes(code: String?) {
        mCheckedCodes.add(code)
        cacheCartCheckedCodes = CacheCartCodes(mCheckedCodes)
    }

    fun cacheCheckedCodes(codes: List<String?>) {
        mCheckedCodes.addAll(codes)
        cacheCartCheckedCodes = CacheCartCodes(mCheckedCodes)
    }

    fun cartHasChecked(code: String?): Boolean {
        return mCheckedCodes.contains(code)
    }

    /** 购物车选中缓存--end */

   // private fun today(): String = DateUtil.formatDate(System.currentTimeMillis(), DateUtil.DATE_FORMAT_Y_M_D)
}

data class CacheCartCodes(
    val list: ArrayList<String?>? = ArrayList()
)